package action.order;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import option.OperationEnum;
import service.OrderService;
import util.ClassUtil;
import util.CookieUtil;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import entity.Cart;
import entity.Order;
import entity.ReceiveAddress;
import entity.User;

/**
 * 订单相关操作
 */
@SuppressWarnings("serial")
public class OrderServlet extends HttpServlet {
	private static Logger log = Logger.getLogger(OrderServlet.class);
	
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String action = (String) request.getAttribute("action");
		if (action.equals(OperationEnum.ORDER_INFO.getValue())) {//跳转到确认订单页面
			request.getRequestDispatcher("/WEB-INF/pages/order/order_info.jsp").forward(request, response);
		} else if (action.equals(OperationEnum.ORDER_ADDRESS_Form.getValue())) {//填写收货地址
			request.getRequestDispatcher("/WEB-INF/pages/order/address_form.jsp").forward(request, response);
		} else if (action.equals(OperationEnum.ORDER_SUCCESS.getValue())) {//订单成功
			User user = (User) request.getSession().getAttribute("user");
			ReceiveAddress ra = ClassUtil.parseRequestAsEntity(request, ReceiveAddress.class);
			Order order = ClassUtil.parseRequestAsEntity(request, Order.class);
			Cart cart = (Cart) request.getSession().getAttribute("cart");
			
			OrderService service = new OrderService();
			try {
				service.saveOrder(user, ra, order, cart);
				//清空购物车
				cart.clear();
				CookieUtil.deleteCookie("cart", response);
			} catch (Exception e) {
				log.error(e);
			}
			
			request.setAttribute("order", order);
			request.getRequestDispatcher("/WEB-INF/pages/order/order_ok.jsp").forward(request, response);
		} else if (action.equals(OperationEnum.ORDER_GET_ADDRESS.getValue())) {//获取收货地址
			User user = (User) request.getSession().getAttribute("user");
			OrderService service = new OrderService();
			List<ReceiveAddress> addresses = new ArrayList<ReceiveAddress>();
			try {
				addresses = service.getAddress(user.getId());
			} catch (Exception e) {
				e.printStackTrace();
			}
			PrintWriter out = response.getWriter();
			if (addresses != null && addresses.size() > 0) {
				out.write(JSONArray.toJSONString(addresses));
			} else {
				out.write("");
			}
			out.close();
		} else if (action.equals(OperationEnum.ORDER_GET_ADDRESS_BY_ID.getValue())) {//根据收货地址id获取单个收货地址
			User user = (User) request.getSession().getAttribute("user");
			String id = request.getParameter("id");
			OrderService service = new OrderService();
			ReceiveAddress ra = null;
			try {
				ra = service.getAddressById(user, Integer.parseInt(id));
			} catch (Exception e){
				e.printStackTrace();
			}
			PrintWriter out = response.getWriter();
			if (ra != null) {
				out.write(JSONObject.toJSONString(ra));
			} else {
				out.write("");
			}
			out.close();
		}
	}
}
